package cby.plugin.check.value

import cby.plugin.check.resource.CheckValueConfig
import cby.plugin.check.resource.ValueInfo
import cby.plugin.check.util.FileUtils
import groovy.xml.XmlParser
import org.gradle.api.Project;

/**
 * value的信息
 */
class ValueChecker {


    private static final Map<String, ValueInfo> stringErrorMap = new HashMap<>()
    private static final Map<String, ValueInfo> colorErrorMap = new HashMap<>()
    private static final Map<String, ValueInfo> dimenErrorMap = new HashMap<>()

    /**
     * 开始检查
     */
    static void startCheckValue(Project project, CheckValueConfig valueConfig) {

        if(valueConfig == null || valueConfig.getNamePrefix() == null
                || valueConfig.getNamePrefix().isEmpty()) {
            // 结束检测
            return
        }

        if(valueConfig.getStringConfig()) {
            new Thread(){

                @Override
                void run() {
                    filterValues(project, valueConfig.getNamePrefix(), 0)
                    if (!stringErrorMap.isEmpty()) {
                        println("String info is error")
                        FileUtils.writeObject(stringErrorMap,"StringError.txt")
                    } else {
                        File file = new File("ResourcesError"+File.separator+"StringError.txt")
                        if(file.exists()){
                            file.delete()
                        }
                        println("String info is ok")
                    }
                    stringErrorMap.clear()
                }
            }.start()
        }

        if(valueConfig.getColorConfig()) {
            new Thread(){

                @Override
                void run() {
                    filterValues(project, valueConfig.getNamePrefix(), 1)
                    if (!colorErrorMap.isEmpty()) {
                        println("Color info is error")
                        FileUtils.writeObject(colorErrorMap,"ColorError.txt")
                    } else {
                        File file = new File("ResourcesError"+File.separator+"ColorError.txt")
                        if(file.exists()){
                            file.delete()
                        }
                        println("Color info is ok")
                    }
                    colorErrorMap.clear()
                }
            }.start()
        }

        if(valueConfig.getDimenConfig()) {
            new Thread(){

                @Override
                void run() {
                    filterValues(project, valueConfig.getNamePrefix(),2)
                    if (!dimenErrorMap.isEmpty()) {
                        println("Dimens info is error")
                        FileUtils.writeObject(dimenErrorMap, "DimenError.txt")
                    } else {
                        File file = new File("ResourcesError"+File.separator+"DimenError.txt")
                        if(file.exists()){
                            file.delete()
                        }
                        println("Dimen info is ok")
                    }
                    dimenErrorMap.clear()
                }
            }.start()
        }
    }

    /**
     * 直接检查当前项目的，字符串或者颜色或者长度值的规范
     * @param project
     * @param prefix
     * @param type
     */
    static void filterValues(Project project, String prefix, int type) {
        if (project.childProjects.size() > 1) {
            project.childProjects.each {
                filterValues(it.value, prefix, type)
            }
        } else {
            switch (type) {
                case 0:
                    filterString("${project.projectDir}/src/main/res/values/strings.xml", prefix)
                    break
                case 1:
                    filterColor("${project.projectDir}/src/main/res/values/colors.xml", prefix)
                    break
                case 2:
                    filterDimen("${project.projectDir}/src/main/res/values/dimens.xml", prefix)
                    break
            }
        }
    }


    static void filterString(String filePath, String prefix){
        File file = new File(filePath)
        if(!file.exists()) {
            return
        }

        def list = new XmlParser().parse(file)
        list.string.each {
            String name = it.@name
            String values = it.text()
            println("String -->" + name + ":" + values)
            if (!"app_name".equals(name)) {
                if (!name.startsWith(prefix)) {
                    stringErrorMap.put(name, new ValueInfo(name, values, filePath).toString())
                }
            }
        }
    }

    static void filterColor(String filePath, String prefix) {
        File file = new File(filePath)
        if (!file.exists()) {
            return
        }
        def list = new XmlParser().parse(filePath)
        list.color.each {
            String name = it.@name
            String values = it.text()
            println("Color -->" + name + ":" + values)
            if (!name.startsWith(prefix)) {
                colorErrorMap.put(name, new ValueInfo(name, values, filePath).toString())
            }
        }
    }

    static void filterDimen(String filePath, String prefix) {
        File file = new File(filePath)
        if (!file.exists()) {
            return
        }
        def list = new XmlParser().parse(filePath)
        list.dimen.each {
            String name = it.@name
            String values = it.text()
            println("Dimen -->" + name + ":" + values)
            if (!name.startsWith(prefix)) {
                dimenErrorMap.put(name, new ValueInfo(name, values, filePath).toString())
            }
        }
    }

}